/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mscs.data;


import java.util.ArrayList;
import java.io.File;
import mscs.data.pojo.Person;
import java.util.List;
import java.util.regex.Pattern;
import junit.framework.Assert;
import mscs.data.FileDataStore;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Usbuild
 */
public class TestFileDataStore {
    FileDataStore fd;
    public TestFileDataStore() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }
    
    @Before
    public void setUp() {
        fd = new FileDataStore(Person.class, new File("p.txt"));
        ArrayList<Person> list = new ArrayList<Person>();
        list.add(new Person(1, "123", "name", 3));
        list.add(new Person(2, "123", "nam", 2));
        list.add(new Person(3, "123", "name", 3));
        try {
            fd.update(list);
        } catch (Exception e) {
            e.printStackTrace();;
        }
    }
    
    @After
    public void tearDown() {
    }

    @Test
    public void testGetObjectsByAttr() throws Exception {
        System.out.println("getObjectsByAttr");
        String key = "id";
        String value = "1";
        FileDataStore instance = fd;
        List result = instance.getObjectsByAttr(key, value);
        assertEquals(((Integer)((Person)result.get(0)).getId()).toString(), value.toString());
    }

    @Test
    public void testSearchObjectsByAttr() throws Exception {
        System.out.println("getObjectsByAttr");
        String key = "name";
        String regexp = "^nam$";
        FileDataStore instance = fd;
        ArrayList<Person> result = (ArrayList<Person>)instance.searchObjectsByAttr(key, regexp);
        Assert.assertTrue(Pattern.compile(regexp).matcher(result.get(0).getName()).matches());
        //assertEquals(((Integer)((Person)result.get(0)).getId()).toString(), regexp);
    }
}
